package threecard.android;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import threecard.android.httpHelper.AccountDbHelper;

public class Forgot extends MainActivity implements OnClickListener {
	public ProgressDialog progressDialog;
	private EditText userEditText;
	private EditText emailEditText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgot);

		// Progress bar.
		
		progressDialog = new ProgressDialog(this);
		// TODO: use a string in resource instead of this.
		progressDialog.setMessage("Please wait ...");
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
		
		
		// initialize to EditText: userEditText and passEditText.
		userEditText = (EditText) findViewById(R.id.forgot_username);
		emailEditText = (EditText) findViewById(R.id.forgot_email);

		View sendButton = findViewById(R.id.send_pass_button);
		sendButton.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.send_pass_button:
			int usersize = userEditText.getText().length();
			int emailsize = emailEditText.getText().length();

			// First check the validity of the username and email
			if (usersize > 0 && emailsize > 0) {
				//progressDialog.show(); // Show progress dialog.
				String user = userEditText.getText().toString(); // get username.
				String email = emailEditText.getText().toString(); // get email.
				doForgot(user, email); // process username and email.
			} else {
				// The user did not enter either username or email.
				// Display an error message.
				createDialog("Error", "Please enter Username and Email");
			}
			break;
		default:
			break;
		}
	}
	
	//TODO: implement login process
		private void doForgot(String user, String email) {
			Log.i("get u & em","ok");
			AccountDbHelper account = new AccountDbHelper( user,null, email);
			boolean result = account.actionForgotPassword();
			if (result == true){
				createDialog("Success","Your password new is 123456, please change after login"); 
				Intent game_intent = new Intent(this, Login.class);
				startActivity(game_intent);
			}else{
				createDialog("Fail","Sorry!! Incorrect Username or Email"); 
			}
		}
		
		
}
